package com.feihao.zhou.TCP.demoFileUpLoadUseThreadPool;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class SocketThread_01 extends Thread {

    public SocketThread_01(Socket accept) {
        this.accept = accept;
    }

    public SocketThread_01(String name, Socket accept) {
        super(name);
        this.accept = accept;
    }

    private Socket accept;
    @Override
    public void run() {
        BufferedOutputStream bos = null;
        BufferedWriter bw = null;
        try {

            InputStream is = accept.getInputStream();
            bos = new BufferedOutputStream(new
                    FileOutputStream("D:\\IdeaProjects\\Job-code\\day16-code-NET\\upLoadDir\\" + UUID.randomUUID().toString() + ".pdf"));

            int len;
            byte[] bytes = new byte[1024];
            while ((len = is.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }

            //写完给客户端回文：
            bw = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
            bw.write("上传成功！！！");
            bw.newLine();
            bw.flush();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                accept.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
